/**
 * 
 */
package fr.isima.tools;

import fr.isima.gui.TrackPreferences;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

/**
 * @author vincent
 */
public class LocationRecorderService extends Service
{

    /** Debug Tag */
    private static final String  TAG = "LocationRecorderService";
    
    /**Broadcast*/
    public static final String NEW_LOCATION_DETECTED = "fr.isima.tools.NEW_LOCATION";
    
    public static final String LONGITUDE = "longitude";
    public static final String LATITUDE = "latitude";

    public static final String RECORDING = "recording";
    
    public static final String ACTION = "action";
    
    
    private int minFreqRefresh;
    private int minDistRefresh;
    
    private LocationManager locManager;
    
    private LocationListener locListener = new LocationListener()
    {
        
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
        }
        
        @Override
        public void onProviderEnabled(String provider)
        {
        }
        
        @Override
        public void onProviderDisabled(String provider)
        {
        }
        
        @Override
        public void onLocationChanged(Location location)
        {
            broadcastLocation(location);
            Log.d(TAG, "Detecting new location (" + location.getLongitude() + ", " + location.getLatitude() + ")");
        }
    };
    
    @Override
    public void onCreate()
    {
        SharedPreferences preferences = getSharedPreferences(TrackPreferences.USER_PREFERENCE, Activity.MODE_PRIVATE);
        
        minFreqRefresh = preferences.getInt(TrackPreferences.PREF_MIN_FREQ_REFRESH, 1) * 60 * 1000;
        minDistRefresh = preferences.getInt(TrackPreferences.PREF_MIN_DIST_REFRESH, 50);
        
        locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                             
        Log.d(TAG, "Service created updating every " + minFreqRefresh + "s and every " + minDistRefresh +"m");
    }


    @Override
    public void onStart(Intent intent, int startId)
    {
        
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minFreqRefresh, minDistRefresh, locListener);
        
    }


    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
    
    private void broadcastLocation(Location location)
    {
        Intent intent = new Intent(NEW_LOCATION_DETECTED);
        intent.putExtra(LONGITUDE, location.getLongitude());
        intent.putExtra(LATITUDE, location.getLatitude());
        
        sendBroadcast(intent);
        Log.d(TAG, "Sending broadcast " + NEW_LOCATION_DETECTED);
    }


    @Override
    public void onDestroy()
    {
        Log.d(TAG, "Stopping the service");
        locManager.removeUpdates(locListener);
        super.onDestroy();
    }
}
